package moriarty.gwt.client;

import java.util.List;

import moriarty.gwt.shared.Submission;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.view.client.ListDataProvider;

/**
 * Tested the idea of making a GUI component a singleton, which will allow easier access for cross component updates.
 * @author cmoriarty
 *
 */
public class PreviousSubmissionTab extends Composite{
	
	private static PreviousSubmissionTab instance = null;
	private final SotdServiceAsync sotdService = GWT.create(SotdService.class);
	private CellTable<Submission> cellTable;
	private ListDataProvider<Submission> dataProvider;

	/*
	 * 
	 */
	public static PreviousSubmissionTab getInstance(){
	     if(instance == null) {
	         instance = new PreviousSubmissionTab();
	      }
	      return instance;
	}
	
	
	protected PreviousSubmissionTab(){
		init();
	}
	
	public void update(){
	    sotdService.retrieveSubmissions(new AsyncCallback<List<Submission>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<Submission> result) {
				dataProvider.setList(result);
			}
	    	
	    });
	}
	
	private void init(){
		
		//Create Columns of the table
		//Date
		Column<Submission,String> dateColumn = new Column<Submission,String>(new TextCell()) {
			@Override
			public String getValue(Submission sub){
				
				//  Months have an 0-11 index so you have to offset them by 1.            //
				//  Years are off by 1900, so you need to offet by them 1900.  			  //
				//  ...pretty ridiculous 												  //  
				@SuppressWarnings("deprecation")
				String niceDate = (sub.date.getYear() + 1900) + "/" + (sub.date.getMonth() + 1) + "/" + sub.date.getDate();
				return niceDate;
			}
		};
		dateColumn.setSortable(true);
		
		//Artist
		Column<Submission,String> artistColumn = new Column<Submission,String>(new TextCell()) {
			@Override
			public String getValue(Submission sub){
				return sub.songArtist;
			}
		};
		artistColumn.setSortable(true);
		
		//Song
		Column<Submission,String> songColumn = new Column<Submission,String>(new TextCell()) {
			@Override
			public String getValue(Submission sub){
				return sub.songName;
			}
		};
		songColumn.setSortable(true);
		
		//Selector
		Column<Submission,String> selectorColumn = new Column<Submission,String>(new TextCell()) {
			@Override
			public String getValue(Submission sub){
				return sub.userName;
			}
		};
		selectorColumn.setSortable(true);
		
		//Comment
		Column<Submission,String> commentColumn = new Column<Submission,String>(new TextCell()) {
			@Override
			public String getValue(Submission sub){
				return sub.comment;
			}
		};
		commentColumn.setSortable(true);
		
		//Link
		Column<Submission,String> linkColumn = new Column<Submission,String>(new ClickableTextCell ()) {
			@Override
			public String getValue(Submission sub){
				return sub.link;
			}
		};
		linkColumn.setSortable(true);
		
		cellTable = new CellTable<Submission>();
		cellTable.addColumn(dateColumn, "Date");
		cellTable.addColumn(artistColumn, "Artist");
		cellTable.addColumn(songColumn, "Song");
		cellTable.addColumn(selectorColumn, "Selector");
		cellTable.addColumn(commentColumn, "Comment");
		cellTable.addColumn(linkColumn, "Link");
		
	    // Create a data provider.
	    dataProvider = new ListDataProvider<Submission>();
	    dataProvider.addDataDisplay(cellTable);
	    sotdService.retrieveSubmissions(new AsyncCallback<List<Submission>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<Submission> result) {
				dataProvider.setList(result);
				
			}
	    	
	    });
		
		initWidget(cellTable);
	}
	
}
